﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.Model;
using System.Data;
public partial class AdminManagement_AddAdminUser : AdminUserBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (SchoolID > 0)
        {

            this.ddlSchoolList.Enabled = false;
            this.trc.Visible = false;
        }
        if (!this.IsPostBack)
        {
            BindSchoolList();
           
        
        if (SchoolID > 0)
        {            
            switch (this.AdminType)
            {
                case 1:
                    this.rbtnListType.Items[0].Enabled = false;
                    this.rbtnListType.Items[1].Enabled = false;
                    this.rbtnListType.Items[2].Enabled = true;
                    this.rbtnListType.Items[3].Enabled = true;
                    this.rbtnListType.Items[4].Enabled = true;
                    break;
                case 2:
                    break;
            }
            getSchoolNameBySId();

        }
        }
        if (SchoolID == 0)
        {
            this.rbtnListType.Items[1].Enabled = true;
            this.rbtnListType.Items[2].Enabled = false;
            this.rbtnListType.Items[3].Enabled = false;
            this.rbtnListType.Items[4].Enabled = false;
        }
    }
    //获取学校名称
    private void getSchoolNameBySId()
    {
        SchoolModule Sm = new SchoolModule();
       V_School vs=Sm.GetSchoolModelById(SchoolID);
       this.ddlSchoolList.Items[0].Text = vs.VS_name.ToString();
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        V_adminUser admin = new V_adminUser();
        admin.VA_loginname = txtUname.Text.Trim();
        admin.VA_password = txtpwd.Text.Trim();
        admin.VA_realname = realname.Text.Trim();
        admin.VA_email = txtemail.Text.Trim();
        admin.VA_mobile = txttelephone.Text.Trim();
        admin.VA_type = Convert.ToInt32(rbtnListType.SelectedValue);
        admin.Create_time = DateTime.Now;
        admin.Update_time = DateTime.Now;
        admin.Va_IsEnable = this.chkIsEnable.Checked ? 1 : 0;

        admin.SchoolID = int.Parse(ddlSchoolList.SelectedValue);
        if (this.AdminType != 0)
            admin.SchoolID = this.SchoolID;
        UserModule userHelper = new UserModule();
        try
        {
            if (int.Parse(rbtnListType.SelectedValue) != 0)
            {
                if (int.Parse(ddlSchoolList.SelectedValue) == 0)
                {
                    if (this.ddlSchoolList.Visible)
                    {
                        lblMess.Text = "请先选择学校！";
                        ddlSchoolList.Enabled = true;
                    }
                    else
                    {
                        int result = userHelper.AddAdminUser(admin);
                        if (result > 0)
                        {
                            PageHelper.AlterMsgAndRedirect("添加成功！", "AdminList.aspx");

                        }
                        else { lblMess.Text = "添加失败，稍后再试！"; }
                    }
                }
                else
                {
                    if (rbtnListType.SelectedValue == null && ddlSchoolList.SelectedValue == null)
                    {
                        if (this.ddlSchoolList.Visible)
                        {
                            lblMess.Text = "请先选择学校！";
                            ddlSchoolList.Enabled = true;
                        }
                    }
                    else
                    {
                        int result = userHelper.AddAdminUser(admin);
                        if (result > 0)
                        {
                            PageHelper.AlterMsgAndRedirect("添加成功！", "AdminList.aspx");

                        }
                        else { lblMess.Text = "添加失败，稍后再试！"; }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblMess.Text = "添加失败，请输入新的登录名，稍后再试！";

        }
        
    }
    public void BindSchoolList()
    {
        SchoolModule schoolHelper = new SchoolModule();
        DataSet dsSchool = schoolHelper.GetSchoolListByWhere("");
        if (dsSchool != null && dsSchool.Tables.Count > 0 && dsSchool.Tables[0].Rows.Count > 0)
        {
            DataRow dr = null;
            ddlSchoolList.Items.Clear();
            ddlSchoolList.Items.Add(new ListItem("请选择", "0"));
            for (int i = 0; i < dsSchool.Tables[0].Rows.Count; i++)
            {
                dr = dsSchool.Tables[0].Rows[i];
                ddlSchoolList.Items.Add(new ListItem(dr["VS_name"].ToString(), dr["ID"].ToString()));
            }

        }
    }
    //public void GetIsAddOrnot()
    //{
    //    string name = txtUname.Text.Trim();
    //    AdminModule admin = new AdminModule();
    //    bool result = admin.IsName(name);
    //    if (result == true)
    //    {
    //        this.AddOrNot.Value = "Tianjia";
    //    }
    //    else
    //    { this.AddOrNot.Value = "chongfu"; }
    //}

}
